//
//  LoopProgressView.m
//  ImagePicker
//
//  Created by YH on 2018/4/26.
//  Copyright © 2018年 王跃. All rights reserved.
//

#import "LoopProgressView.h"
#import <QuartzCore/QuartzCore.h>

#define K_basicAnimationkey     @"basicAnimationkey"
#define ViewWidth               self.frame.size.width       //环形进度条的视图宽度
#define ProgressWidth           4.f                         //环形进度条的圆环宽度
#define Radius                  ViewWidth/2 - ProgressWidth //环形进度条的半径
#define ViewcenterX             self.frame.size.width / 2.0
#define ViewcenterY             self.frame.size.height / 2.0


@interface  LoopProgressView()<CAAnimationDelegate>

@property (nonatomic, strong) CAShapeLayer  *circleShapeLayer;

@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGestureRecognizer;

@end

@implementation LoopProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addLoogPressGestureRecognizer];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef progressContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(progressContext, ProgressWidth);
    CGContextSetRGBStrokeColor(progressContext, 0, 0, 0, 0);
    //绘制环形进度条底框
    CGContextAddArc(progressContext, ViewcenterX, ViewcenterY, Radius, 0, 2 * M_PI, 0);
    CGContextDrawPath(progressContext, kCGPathStroke);
}

- (void)addCirclePath
{
    // //绘制环形进度环
    CGFloat endAngle =  M_PI * 2;
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path addArcWithCenter:CGPointMake(ViewcenterX,ViewcenterY) radius:Radius startAngle:- M_PI * 0.5 endAngle:endAngle clockwise:YES];
    self.circleShapeLayer = [CAShapeLayer layer];
    self.circleShapeLayer.path = path.CGPath;//46,169,230
    self.circleShapeLayer.strokeColor = [UIColor colorWithRed:103.0/255.0 green:180.0/255.0 blue:0.0/255.0 alpha:1].CGColor;
    self.circleShapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.circleShapeLayer.lineWidth = ProgressWidth;
    self.circleShapeLayer.backgroundColor = [UIColor blueColor].CGColor;
    [self.layer addSublayer:self.circleShapeLayer];

}

#pragma mark  动画

-(void)drawLineAnimation:(CALayer*)layer
{
    CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    basicAnimation.duration=10.f;//动画时间
    basicAnimation.delegate=self;
    basicAnimation.fromValue=[NSNumber numberWithInteger:0];
    basicAnimation.toValue=[NSNumber numberWithInteger:1];
    [layer addAnimation:basicAnimation forKey:K_basicAnimationkey];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [self removeAnimation];
    if (flag) {
         [self.longPressGestureRecognizer cancelsTouchesInView];
        if (self.delegate && [self.delegate respondsToSelector:@selector(loopProgressViewRecordedState:)]) {
            [self.delegate loopProgressViewRecordedState:LoopProgressViewStateEnd];
        }
    }
}

- (void)removeAnimation
{
    [self.circleShapeLayer removeAnimationForKey:K_basicAnimationkey];
}

#pragma mark  添加手势

- (void)addLoogPressGestureRecognizer
{
    self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressGestureRecognizer:)];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:self.longPressGestureRecognizer];
}

- (void)LongPressGestureRecognizer:(UIGestureRecognizer *)recognizer
{
    LoopProgressViewState loopProgressViewState = LoopProgressViewStateUnkonwn;
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        loopProgressViewState = LoopProgressViewStateBegin;
        [self addCirclePath];
        [self drawLineAnimation:self.circleShapeLayer];
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
         loopProgressViewState = LoopProgressViewStateEnd;
         [self removeAnimation];
    }else if (recognizer.state == UIGestureRecognizerStateCancelled){
         loopProgressViewState = LoopProgressViewStateCancel;
        [self removeAnimation];

    }

    if (self.delegate && [self.delegate respondsToSelector:@selector(loopProgressViewRecordedState:)]) {
        [self.delegate loopProgressViewRecordedState:loopProgressViewState];
    }
}

#pragma mark  自定义事件

- (void)removeCircleShapeLayer
{
    [self.circleShapeLayer removeFromSuperlayer];
    self.circleShapeLayer = nil;
    [[UIBezierPath bezierPath] removeAllPoints];
}

@end
